ggmap
The maps
package produces pretty rudimentary maps which you can use for your visualizations. ggmap
is the package that you’ll want to use if you want a more detailed map as background to your visualizations. In what follows, we’ll demonstrate how to use ggmap
with the Sacramento
dataset in the caret
package:
library(caret)
library(dplyr)
library(ggmap)
data(Sacramento)
df <- Sacramento %>% group_by(city) %>%
summarize(median_price = median(price), transactions = n(),
latitude = mean(latitude), longitude = mean(longitude))
It turns out that using Google maps as a background is quite a challenge now. To do so, you need to provide an API key and enable billing. See https://github.com/dkahle/ggmap for more details on how to work with these new restrictions.
Fortunately there is an easier way to get maps which are more sophisticated than those provided by the maps
package: Stamen maps. The code below is an example of how to get a map from Stamen maps (the get_stamenmap
function belongs to the ggmap
package.)
height <- max(Sacramento$latitude) - min(Sacramento$latitude)
width <- max(Sacramento$longitude) - min(Sacramento$longitude)
sac_borders <- c(bottom = min(Sacramento$latitude) - 0.1 * height,
top = max(Sacramento$latitude) + 0.1 * height,
left = min(Sacramento$longitude) - 0.1 * width,
right = max(Sacramento$longitude) + 0.1 * width)
map <- get_stamenmap(sac_borders, zoom = 10, maptype = "toner-lite")
## Source : http://tile.stamen.com/toner-lite/10/165/390.png
## Source : http://tile.stamen.com/toner-lite/10/166/390.png
## Source : http://tile.stamen.com/toner-lite/10/167/390.png
## Source : http://tile.stamen.com/toner-lite/10/168/390.png
## Source : http://tile.stamen.com/toner-lite/10/169/390.png
## Source : http://tile.stamen.com/toner-lite/10/165/391.png
## Source : http://tile.stamen.com/toner-lite/10/166/391.png
## Source : http://tile.stamen.com/toner-lite/10/167/391.png
## Source : http://tile.stamen.com/toner-lite/10/168/391.png
## Source : http://tile.stamen.com/toner-lite/10/169/391.png
## Source : http://tile.stamen.com/toner-lite/10/165/392.png
## Source : http://tile.stamen.com/toner-lite/10/166/392.png
## Source : http://tile.stamen.com/toner-lite/10/167/392.png
## Source : http://tile.stamen.com/toner-lite/10/168/392.png
## Source : http://tile.stamen.com/toner-lite/10/169/392.png
## Source : http://tile.stamen.com/toner-lite/10/165/393.png
## Source : http://tile.stamen.com/toner-lite/10/166/393.png
## Source : http://tile.stamen.com/toner-lite/10/167/393.png
## Source : http://tile.stamen.com/toner-lite/10/168/393.png
## Source : http://tile.stamen.com/toner-lite/10/169/393.png
## Source : http://tile.stamen.com/toner-lite/10/165/394.png
## Source : http://tile.stamen.com/toner-lite/10/166/394.png
## Source : http://tile.stamen.com/toner-lite/10/167/394.png
## Source : http://tile.stamen.com/toner-lite/10/168/394.png
## Source : http://tile.stamen.com/toner-lite/10/169/394.png
ggmap(map)
Notice that a bunch of messages are printed showing what files are being loaded. To remove these messages from your report, include the option “message=FALSE” at the top of your R chunk.
By default, the zoom parameter is set to 10. Making it bigger gives a more detailed map, but takes longer to load.
Just like in ggplot2
graphics, we can add layers to the map drawn by ggmap(map)
with the +
syntax. For example, if we wanted to overlay the map with the data from our Sacramento
dataset, we could do the following:
ggmap(map) +
geom_point(data = df, mapping = aes(x = longitude, y = latitude,
col = median_price, size = transactions)) +
scale_color_distiller(palette = "YlOrRd", direction = 1)
Another way to get Stamen maps as a background is to use the qmplot
function. Notice though that the syntax is pretty different from what we had with ggplot
. Here is a map plotting the same points as above but with a different color scale:
qmplot(x = longitude, y = latitude, data = df, maptype = "watercolor",
geom = "point", color = median_price, size = transactions) +
scale_color_gradient(low = "blue", high = "red")
The full list of map types is “terrain”, “terrain-background”, “terrain-labels”, “terrain-lines”, “toner”, “toner-2010”, “toner-2011”, “toner-background”, “toner-hybrid”, “toner-labels”, “toner-lines”, “toner-lite”, “watercolor”. Note that some of them don’t work with the CRAN version of ggmap
(e.g. “terrain”); instead, you will have to download the github version of the package (see https://github.com/dkahle/ggmap/issues/188).
Below are examples of what “terrain” and “toner” look like:
qmplot(x = longitude, y = latitude, data = df, maptype = "terrain",
geom = "point", color = median_price, size = transactions) +
scale_color_gradient(low = "blue", high = "red")
qmplot(x = longitude, y = latitude, data = df, maptype = "toner",
geom = "point", color = median_price, size = transactions) +
scale_color_gradient(low = "blue", high = "red")
sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggmap_3.0.0 dplyr_0.8.3 caret_6.0-84 ggplot2_3.2.1
## [5] lattice_0.20-38
##
## loaded via a namespace (and not attached):
## [1] tidyselect_0.2.5 xfun_0.9 purrr_0.3.2
## [4] reshape2_1.4.3 splines_3.6.1 colorspace_1.4-1
## [7] generics_0.0.2 htmltools_0.3.6 stats4_3.6.1
## [10] yaml_2.2.0 survival_2.44-1.1 prodlim_2018.04.18
## [13] rlang_0.4.0 ModelMetrics_1.2.2 pillar_1.4.2
## [16] glue_1.3.1 withr_2.1.2 RColorBrewer_1.1-2
## [19] jpeg_0.1-8 foreach_1.4.7 plyr_1.8.4
## [22] lava_1.6.6 stringr_1.4.0 timeDate_3043.102
## [25] munsell_0.5.0 gtable_0.3.0 RgoogleMaps_1.4.4
## [28] recipes_0.1.7 codetools_0.2-16 evaluate_0.14
## [31] labeling_0.3 knitr_1.24 curl_4.0
## [34] class_7.3-15 Rcpp_1.0.2 scales_1.0.0
## [37] ipred_0.9-9 rjson_0.2.20 png_0.1-7
## [40] digest_0.6.20 stringi_1.4.3 grid_3.6.1
## [43] bitops_1.0-6 tools_3.6.1 magrittr_1.5
## [46] lazyeval_0.2.2 tibble_2.1.3 tidyr_0.8.3
## [49] crayon_1.3.4 pkgconfig_2.0.2 MASS_7.3-51.4
## [52] Matrix_1.2-17 data.table_1.12.2 lubridate_1.7.4
## [55] gower_0.2.1 httr_1.4.1 assertthat_0.2.1
## [58] rmarkdown_1.15 iterators_1.0.12 R6_2.4.0
## [61] rpart_4.1-15 nnet_7.3-12 nlme_3.1-140
## [64] compiler_3.6.1